package net.admin.action;

import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.admin.db.article.ArticleBean;
import net.admin.db.article.ArticleDAO;
import net.admin.db.item.CategoryDAO;
import net.comm.Property;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

public class ArticleRegistAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ActionForward af = new ActionForward();
		af.setRedirect(false);
		ArticleDAO dao = null;
		String article_code=null;
		
		af.setPath("/admin/article/article_manage.jsp");
		HttpSession session = request.getSession();
		String admin_id=(String)session.getAttribute("user_id");
		ArticleBean ab = new ArticleBean();
		String image_path=System.currentTimeMillis()+"";
		File upload_dir=new File(Property.workspace+request.getContextPath()+"/WebContent/admin/article_image/"+image_path);
		if(upload_dir.exists()==false)upload_dir.mkdir();
		System.out.println(upload_dir);
		
		
		try{
			MultipartRequest multi = new MultipartRequest(request,upload_dir.getAbsolutePath(),4*1024*1024,"euc-kr", new DefaultFileRenamePolicy());
			
			String item_code=multi.getParameter("item_cd");
			String article_price=multi.getParameter("article_price");
			String article_nm=multi.getParameter("article_nm");
			String article_manual=multi.getParameter("manual");
			String title_image=multi.getFilesystemName("title_image");
			String [] sub_images=getSubImages(multi);
			
			ab.setImage_path(image_path);
			ab.setArticle_price(article_price);
			ab.setItem_code(item_code);
			ab.setArticle_nm(article_nm);
			ab.setArticle_manual(article_manual);
			ab.setTitle_image(title_image);
			ab.setSub_images(sub_images);
			ab.setAdmin_id(admin_id);
			
			
			dao = new ArticleDAO();
			article_code=dao.registArticle(ab);
			if(article_code!=null){
				request.setAttribute("result","Y");
				request.setAttribute("article_code",article_code);
			}else{
				request.setAttribute("result","N");
			}
		}catch(Exception e){
			e.printStackTrace();
			request.setAttribute("result", "N");
		}finally{
			if(dao!=null)dao.close();
		}
		
		return af;
	}
	private String [] getSubImages(MultipartRequest multi){
		ArrayList sub_image_set = new ArrayList();
		String [] sub_images=null;
		String fileName=null;
		Enumeration e = multi.getFileNames();
		while(e.hasMoreElements()){
			String param = e.nextElement().toString();
			fileName=multi.getFilesystemName(param);
			if(param.indexOf("sub_image")>-1 && fileName!=null){
				sub_image_set.add(fileName);
			}
		}
		sub_images = new String[sub_image_set.size()];
		for(int i=sub_image_set.size()-1,j=0;i>=0;i--,j++){
			sub_images[j]=(String)sub_image_set.get(i);
		}
		
		return sub_images;
	}

}
